package com.itzxx.huazhun.ui.device.list

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.chad.library.adapter4.QuickAdapterHelper
import com.chad.library.adapter4.loadState.LoadState
import com.chad.library.adapter4.loadState.trailing.TrailingLoadStateAdapter
import com.itzxx.huazhun.R
import com.itzxx.huazhun.data.model.DevicesChildBean
import com.itzxx.huazhun.databinding.ActivityDevicesListBinding
import com.itzxx.huazhun.view.adapter.CustomLoadMoreAdapter
import com.itzxx.library_base_ktx.base.activity.BaseVmDbActivity
import com.itzxx.library_base_ktx.ext.view.addLinearAdapter

class DevicesListActivity : BaseVmDbActivity<DeviceListViewModel, ActivityDevicesListBinding>() {

    companion object {
        fun startActivity(context: Context, id: String) {
            val instent = Intent(
                context,
                DevicesListActivity()::class.java
            )
            instent.putExtra("recordId", id)
            context.startActivity(
                instent
            )
        }

        fun startActivity(context: Context, ids: ArrayList<Int>) {
            val instent = Intent(
                context,
                DevicesListActivity()::class.java
            )
            instent.putExtra("recordIdList", ids)
            context.startActivity(
                instent
            )
        }
    }

    private lateinit var devicesAdapter: DevicesListAdapter
    private lateinit var helper: QuickAdapterHelper
    private lateinit var loadMoreAdapter: CustomLoadMoreAdapter


    override fun initView(savedInstanceState: Bundle?) {
        setSupportActionBar(mDatabind.tbHead)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        //单个
        intent.getStringExtra("recordId")?.let { mViewModel.recordIdList.add(it.toInt()) }
        //多个
        intent.getIntegerArrayListExtra("recordIdList")?.let {  mViewModel.recordIdList.addAll(it)}
        initRecyclerView()
    }

    override fun createObserver() {
        mViewModel.run {
            listDataUiStateLiveData.observe(this@DevicesListActivity) {
                if (it.isRefresh) {
                    devicesAdapter.submitList(it.listData)
                } else {
                    devicesAdapter.addAll(it.listData)
                }

                if (it.hasMore) {
                    helper.trailingLoadState = LoadState.NotLoading(false)
                } else {
                    loadMoreAdapter.setFirstNotFullScreen(it.isRefresh)
                    helper.trailingLoadState = LoadState.NotLoading(true)
                }
            }
        }
    }

    private fun initRecyclerView() {
        devicesAdapter = DevicesListAdapter()
        loadMoreAdapter = CustomLoadMoreAdapter()
        loadMoreAdapter.setOnLoadMoreListener(object : TrailingLoadStateAdapter.OnTrailingListener {
            override fun onLoad() {
                // 执行加载更多的操作，通常都是网络请求
                mViewModel.getList(false)
            }

            override fun onFailRetry() {
                // 加载失败后，点击重试的操作，通常都是网络请求
                mViewModel.getList(false)
            }

            override fun isAllowLoading(): Boolean {
                return true
            }
        })
        helper = QuickAdapterHelper.Builder(devicesAdapter)
            .setTrailingLoadStateAdapter(loadMoreAdapter)
            .build()
        helper.trailingLoadState = LoadState.None
        mDatabind.rvList.addLinearAdapter(helper.adapter)
        devicesAdapter.addOnItemChildClickListener(R.id.btn_relation) { adapter, view, position ->
            val item = devicesAdapter.getItem(position) as DevicesChildBean
            mViewModel.bindProbeDataById(item)
        }
    }


    override fun isApplyDefaultWaitDialog(): Boolean {
        return true
    }

    override fun databind2ViewModel() {
        super.databind2ViewModel()
        mDatabind.viewmodel = mViewModel
    }

    override fun onSupportNavigateUp(): Boolean {
        finish()
        return true
    }
}